﻿using System;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using EmptyWallGallery.Core;
using EmptyWallGallery.Core.Utilities;

namespace EmptyWallGallery.FrontEnd.inside.Editor.Gallery
{
    public partial class CreatePhoto : Page
    {
        protected string UserId { get; set; }
        protected string UserName { get; set; }
        protected string AlbumId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Request.QueryString["AlbumId"] == null) Response.Redirect("Album.aspx");
            if (!Common.Instance.IsNumeric(Request.QueryString["AlbumId"])) Response.Redirect("Album.aspx");

            AlbumId = Request.QueryString["AlbumId"];

            BindData(Convert.ToInt32(AlbumId), 0);

            var albumItem = Core.BLL.Album.Instance.GetById(Convert.ToInt32(Request.QueryString["AlbumId"]));
            headTitle.InnerText += ": " + albumItem.AlbumName;

            Page.Title = "Thêm tác phẩm - " + albumItem.AlbumName + " - " + Configs.WebTitle;
        }

        public void BindData(int albumId, int status)
        {
            lstPhoto.DataSource = Core.BLL.Images.Instance.GetByAlbum(status, albumId, true);
            lstPhoto.DataBind();
        }

        protected string WriteImage(string fileName, DateTime createDate, string uploadBy)
        {
            var objUser = Membership.GetUser(Guid.Parse(uploadBy));
            if (objUser == null) return "";

            var strImageDefault = fileName == ""
                                      ? "/images/no_image_s.gif"
                                      : "/Uploads/" + objUser.UserName + "/" + createDate.Year + "/" +
                                        Common.Instance.Right("0" + createDate.Month, 2) + "/" +
                                        Common.Instance.Right("0" + createDate.Day, 2) + "/";

            return "<a href=\"" + strImageDefault + "700x0__" + fileName + "\" rel=\"TopProduct\"><img src=\"" +
                   strImageDefault + "135x0__" + fileName + "\" border=\"0\" width=\"130\" /></a>";
        }

        protected string WriteCreateDate(string toString)
        {
            return DateTime.Parse(toString).AddHours(Configs.TimeLine).ToString("dd-MM-yyyy");
        }

        protected void LstPhotoPreRender(object sender, EventArgs e)
        {
            if (lstPhoto.Items.Count == 0)
            {
                nophoto.InnerHtml = "* Không có tác phẩm nào.";
            }
        }

        protected void BtnCreateClick(object sender, EventArgs e)
        {
            var strFileName = UploadPhoto();
            if (strFileName == "") return;

            var strFileNameOri = Common.Instance.Utf8ToAscii(fileUpload.FileName).ToLower().Replace("-jpg", "");
            UserName = HttpContext.Current.User.Identity.Name;
            UserId = Membership.GetUser(UserName).ProviderUserKey.ToString();

            Core.BLL.Images.Instance.Create(strFileNameOri, Common.Instance.FormatMultiline(txtDesc.Text), strFileName, Convert.ToInt32(Request.QueryString["AlbumId"]),
                UserId);

            Response.Redirect("CreatePhoto.aspx?AlbumId=" + Request.QueryString["AlbumId"]);
        }

        public string UploadPhoto()
        {
            var rnd = new RandomStrings();
            var strFileName = rnd.GenerateRandomString(DateTime.Now) + ".jpg";
            var strFolderUpload = Configs.UserFolder + HttpContext.Current.User.Identity.Name + @"\" + DateTime.Now.Year + @"\" +
                                  Common.Instance.Right("0" + DateTime.Now.Month, 2) + @"\" +
                                  Common.Instance.Right("0" + DateTime.Now.Day, 2) + @"\";


            if (!Directory.Exists(strFolderUpload))
            {
                Directory.CreateDirectory(strFolderUpload);
            }
            fileUpload.SaveAs(strFolderUpload + "o__" + strFileName);

            if (ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + strFileName, 1024, 0, "") &&
                ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + "700x0__" + strFileName, 700, 0, "") &&
                ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + "640x0__" + strFileName, 640, 0, "") &&
                ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + "500x0__" + strFileName, 500, 0, "") &&
                ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + "180x0__" + strFileName, 160, 0, "") &&
                ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + "135x0__" + strFileName, 135, 0, "") &&
                ImageUtilities.ResizeImage(fileUpload.PostedFile.InputStream, strFolderUpload + "51x0__" + strFileName, 51, 51, ""))
            {
                return strFileName;
            }
            return "";
        }
    }
}